package com.wzh.live.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.wzh.common.utils.R;
import com.wzh.live.vo.NewHouseVo;
import com.wzh.live.feign.HouseFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;

import com.wzh.live.dao.LiveHouseDao;
import com.wzh.live.entity.LiveHouseEntity;
import com.wzh.live.service.LiveHouseService;


@Service("liveHouseService")
public class LiveHouseServiceImpl extends ServiceImpl<LiveHouseDao, LiveHouseEntity> implements LiveHouseService {

    @Autowired
    private HouseFeign houseFeign;

    @Autowired
    private LiveHouseDao liveHouseDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<LiveHouseEntity> page = this.page(
                new Query<LiveHouseEntity>().getPage(params),
                new QueryWrapper<LiveHouseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<NewHouseVo> getsByLiveId(Integer liveId) {

        List<LiveHouseEntity> live_id = baseMapper.selectList(new QueryWrapper<LiveHouseEntity>().eq("live_id", liveId));
        List<NewHouseVo> collect = live_id.stream().map(item -> {
            R byId = houseFeign.getById(item.getHouseId());
            NewHouseVo data = byId.getData(new TypeReference<NewHouseVo>() {
            });
            data.setHousePrice(item.getPrice());
            return data;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void updateByIds(List<LiveHouseEntity> collect) {
        collect.stream().forEach(item->{
            Integer i=liveHouseDao.updateByIds(item);
        });

    }

    @Override
    public void removeByLiveId(Integer liveId) {
        baseMapper.removeByLiveId(liveId);
    }

    @Override
    public void removeByLiveIds(Integer[] liveIds) {
        baseMapper.removeByLiveIds(liveIds);
    }

}